package iso3.pt.action;



import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class StudentAction  extends ActionSupport implements Preparable{
	
	private Alumno alu = null;
	private ArrayList<Asignatura> asigs;
	private Set<Asignatura> temp,temp2;
	private ArrayList<Asignatura> asigsSinMatricular;
	private PtDaoService dao = null;
	private int subjectId = -1;
	private int asigID;
	
	
	public String doLogout() throws Exception{
		Map s = ActionContext.getContext().getSession();
		s.remove("logged");		
		return INPUT;
	}

	@Override
	public void prepare() throws Exception {
		if (dao == null){
			dao = new PtDaoService();
		}
		try {
			alu = (Alumno)ActionContext.getContext().getSession().get("logged");
			temp = dao.getAsignaturas(alu.getId());
			asigs = new ArrayList<Asignatura>(temp);
			temp2 = dao.getAsignaturas();
			asigsSinMatricular = new ArrayList<Asignatura>(temp2);
			for (int i = 0; i < asigs.size();i++)
				asigsSinMatricular.remove(asigs.get(i));
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
	}
	
	public String doStudent(){
		return SUCCESS;
	}
	
	public String enroll() throws Exception{
		if (subjectId != -1) {
			dao.matricular(alu.getId(), subjectId);
		}
		subjectId = -1;
		
		prepare();
		
		return "enroll";
	}
	
	public String disenroll() throws Exception {			
		int subjectId = Integer.parseInt(((String[])ActionContext.getContext().getParameters().get("subjectId"))[0]);	
		dao.desmatricular(alu.getId(), subjectId);
		
		prepare();
		
		return "disenroll";	
	}	

	public ArrayList<Asignatura> getAsigs() {
		return asigs;
	}
	
	public void setAsigs(ArrayList<Asignatura> asigs) {
		this.asigs = asigs;
	}
	
	public PtDaoService getDao() {
		return dao;
	}

	public void setDao(PtDaoService dao) {
		this.dao = dao;
	}
	
	public Alumno getAlu() {
		return alu;
	}

	public void setAlu(Alumno alu) {
		this.alu = alu;
	}

	public ArrayList<Asignatura> getAsigsSinMatricular() {
		return asigsSinMatricular;
	}

	public void setAsigsSinMatricular(ArrayList<Asignatura> asigsSinMatricular) {
		this.asigsSinMatricular = asigsSinMatricular;
	}

	public void setSubjectId(int subjectId) {
		this.subjectId = subjectId;
	}

	public int getSubjectId() {
		return subjectId;
	}

	public void setAsigID(int asigID) {
		this.asigID = asigID;
	}

	public int getAsigID() {
		return asigID;
	}

}
